package org.micropayment.model.service.jpa;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.micropayment.model.MicropaymentConsumer;
import org.micropayment.model.service.MicropaymentConsumerDAO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.context.annotation.Scope;

/**
 *
 * @author jj
 */
@Repository("consumerDao")
@Scope("prototype")
public class JpaMicropaymentConsumerDAO implements MicropaymentConsumerDAO {

    private static Logger logger =
            LoggerFactory.getLogger(JpaMicropaymentConsumerDAO.class);

    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    public MicropaymentConsumer findById(Integer id) {
        return entityManager.find(MicropaymentConsumer.class, id);
    }

    public List<MicropaymentConsumer> findAll() {
        Query query = 
                entityManager.createNamedQuery("MicropaymentConsumer.findAll");
        List<MicropaymentConsumer> consumers  = query.getResultList();
        return consumers;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public MicropaymentConsumer save(MicropaymentConsumer consumer) {
        entityManager.persist(consumer);
        entityManager.flush();
        return consumer;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public MicropaymentConsumer update(MicropaymentConsumer consumer) {
        entityManager.merge(consumer);
        entityManager.flush();
        return consumer;
    }

    @Transactional(readOnly=false, propagation=Propagation.REQUIRED)
    public boolean delete(MicropaymentConsumer consumer) {
        consumer = entityManager.getReference(MicropaymentConsumer.class, consumer.getId());
        if (consumer == null) return false;
        entityManager.remove(consumer);
        entityManager.flush();
        return true;
    }

    public List<MicropaymentConsumer> findList(int firstResult, int maxResults) {
        Query query = entityManager.createQuery(
                "SELECT c FROM MicropaymentConsumer c");
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        List<MicropaymentConsumer> users = query.getResultList();
        return users;
    }


    public MicropaymentConsumer findByConsumerKey(String consumerKey) {
    	MicropaymentConsumer result = null;
        Query query = entityManager.createNamedQuery("MicropaymentConsumer.findByConsumerKey");
        query.setParameter("consumerKey", consumerKey);
        List<MicropaymentConsumer> consumers = query.getResultList();
        if (!consumers.isEmpty()) result = consumers.get(0);
        return result;
    }

}
